// src/utils/dialog.js
import Vue from 'vue';
import DialogComponent from '@/components/common/dialog/index.vue';

let instance = null;

function getInstance() {
  if (!instance) {
    // 创建子类
    const DialogConstructor = Vue.extend(DialogComponent);

    // 创建实例
    instance = new DialogConstructor({
      // ✅ 使用 propsData 传入初始值
      propsData: {
        visible: false,
        title: '',
        content: '',
        onConfirm: () => {},
        onCancel: () => {},
      },
    });

    // 手动挂载
    instance.$mount();
    document.body.appendChild(instance.$el);
  }
  return instance;
}

function showDialog(options) {
  const instance = getInstance();

  // 合并配置
  const config = {
    title: options.title || '提示',
    content: options.content || '是否要删除？',
    onConfirm: options.onConfirm || (() => {}),
    onCancel: options.onCancel || (() => {}),
  };

  // ✅ 更新 propsData（通过 $props 不直接赋值）
  instance.title = config.title;
  instance.content = config.content;
  instance.onConfirm = config.onConfirm;
  instance.onCancel = config.onCancel;

  // ✅ 显示对话框
  instance.visible = true;

  return instance;
}

export default showDialog;
